package cn.akwangl.spring.ioc.dependency.lookup;

import cn.akwangl.spring.ioc.annotation.Super;
import cn.akwangl.spring.ioc.model.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;

/**
 * 依赖查找Demo
 *
 * @author youyongkun
 * @date 2021/6/24 15:04
 */
public class DependencyLookupDemo {

    public static void main(String[] args) {
        String xmlPath = "META-INF/dependency-lookup-context.xml";
        ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext(xmlPath);
        beanFactory.refresh();

        lookupInRealTime(beanFactory);
        lookupInLazy(beanFactory);

        lookupByType(beanFactory);
        lookupCollectionByType(beanFactory);

        lookupByNameAndType(beanFactory);

        lookupByAnnotationCollection(beanFactory);
    }

    /**
     * 实时查找
     *
     * @param beanFactory IOC容器
     * @author youyongkun
     * @date 2021/6/24 15:08
     */
    public static void lookupInRealTime(BeanFactory beanFactory) {
        User user = (User) beanFactory.getBean("user");
        System.out.printf("Spring IOC 实时查找 %s \n", user);
    }

    /**
     * Spring IOC 延迟查找{@link ObjectFactory}
     *
     * @param beanFactory IOC容器
     * @author youyongkun
     * @date 2021/6/24 15:22
     */
    public static void lookupInLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
        System.out.printf("Spring IOC 延迟查找 %s \n", objectFactory.getObject());
    }

    /**
     * Spring IOC 单个Bean类型查找
     *
     * @param beanFactory IOC容器
     * @author youyongkun
     * @date 2021/6/24 15:28
     */
    public static void lookupByType(BeanFactory beanFactory) {
        User user = beanFactory.getBean(User.class);
        System.out.printf("Spring IOC 类型查找 %s \n", user);
    }

    /**
     * Spring IOC 容器 集合Bean类型查找{@link ListableBeanFactory}
     *
     * @param beanFactory IOC容器
     * @author youyongkun
     * @date 2021/6/24 15:30
     */
    public static void lookupCollectionByType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);
            System.out.printf("Spring IOC 集合类型查找 %s \n", users);
        }
    }

    /**
     * Spring IOC 根据Bean名称+类型查找
     *
     * @param beanFactory Spring IOC
     * @author youyongkun
     * @date 2021/6/24 15:34
     */
    public static void lookupByNameAndType(BeanFactory beanFactory) {
        User user = beanFactory.getBean("user", User.class);
        System.out.printf("Spring IOC 根据Bean名称+类型查找 %s \n", user);
    }

    /**
     * Spring IOC 查找标注{@link Super}注解Bean的集合
     *
     * @param beanFactory IOC容器
     * @author youyongkun
     * @date 2021/6/24 16:30
     */
    public static void lookupByAnnotationCollection(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listenerBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = (Map) listenerBeanFactory.getBeansWithAnnotation(Super.class);
            System.out.printf("Spring IOC 按照注解查找Bean集合 %s \n", users);
        }
    }
}
